package com.wanmait.baohan.controller.workTemplateController;

import com.github.pagehelper.PageInfo;
import com.wanmait.baohan.pojo.*;
import com.wanmait.baohan.service.*;
import com.wanmait.baohan.vo.DataVo;
import com.wanmait.baohan.vo.KeyAndValueVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * TODO
 *
 * @author 打豆豆
 * @version 1.0
 * @date 2021/2/12 14:07
 * @description:
 */
@Controller
@RequestMapping("/work-template")
public class TemplateManageController {

    @Resource
    private WorkTemplateService workTemplateService;
    @Resource
    private WorkTemplateContentService workTemplateContentService;
    @Resource
    private DictionaryService dictionaryService;
    @Resource
    private RoleService roleService;
    @Resource
    private WorkTemplateExecutableService workTemplateExecutableService;


    @RequestMapping("createWorkTemplate")
    public String createWorkTemplate(){
        return "work-template/createWorkTemplate";
    }

    @RequestMapping("createWorkTemplateContents")
    public String createWorkTemplateContents(){
        return "work-template/createWorkTemplateContents";
    }

    @RequestMapping("tableTemplates")
    public String tableTemplates(){
        return "work-template/tableTemplates";
    }

    @RequestMapping("getWorkTemplates")
    @ResponseBody
    public DataVo<WorkTemplate> getWorkTemplates(Integer page, Integer limit){
        DataVo<WorkTemplate> workTemplateDataVo = new DataVo<>();
        PageInfo<WorkTemplate> pageInfo = workTemplateService.SelectAllWorkTemplate(page, limit);
        workTemplateDataVo.setData(pageInfo.getList());
        workTemplateDataVo.setCount(pageInfo.getTotal());
        return workTemplateDataVo;
    }
    @RequestMapping("getWorkTemplates2")
    @ResponseBody
    public List<WorkTemplate> getWorkTemplates2(){
        return  workTemplateService.findAll();
    }

    @RequestMapping("workTemplateDetails")
    public String workTemplateDetails(){
        return "work-template/workTemplateDetails";
    }

    @RequestMapping("getWorkTemplateContents")
    @ResponseBody
    public List<WorkTemplateContent> getWorkTemplateContents(Integer id){
        return workTemplateContentService.findByWorkTemplateId(id);
    }
    @RequestMapping("createWorkTemplateExecutable")
    @ResponseBody
    public List<WorkTemplateExecutable> createWorkTemplateExecutable(Integer id){
        return workTemplateContentService.createWorkTemplateExecutableByWorkTemplate(id);
    }

    @RequestMapping("getTemplateTypes")
    @ResponseBody
    public List<KeyAndValueVo> getTemplateTypes(){
        return dictionaryService.getTemplateTypes();
    }

    @RequestMapping("getUser")
    @ResponseBody
    public Staff getUser(HttpServletRequest request){
        return (Staff)request.getSession().getAttribute("user");
    }

    @RequestMapping("uploadNewTemplate")
    @ResponseBody
    public String uploadNewTemplate(@RequestBody WorkTemplate workTemplate){
        try {
            workTemplateService.insert(workTemplate);
            return "0";
        }catch (Exception e){
            return "1";
        }

    }
    @RequestMapping("updateTemplateContent")
    @ResponseBody
    public String updateTemplateContent(@RequestBody WorkTemplateContent workTemplateContent){
        try {
            workTemplateContentService.updateTemplateContent(workTemplateContent);
            return "0";
        }catch (Exception e){
            return "1";
        }
    }
    @RequestMapping("insertTemplateContent")
    @ResponseBody
    public String insertTemplateContent(@RequestBody WorkTemplateContent workTemplateContent){
        try {
            workTemplateContentService.insertTemplateContent(workTemplateContent);
            return "0";
        }catch (Exception e){
            return "1";
        }

    }
    @RequestMapping("getLevel")
    @ResponseBody
    public List<KeyAndValueVo> getLevel(){
        return dictionaryService.getWorkLevel();
    }
    @RequestMapping("getWorkload")
    @ResponseBody
    public List<KeyAndValueVo> getWorkload(){
        return dictionaryService.getWorkload();
    }
    @RequestMapping("getRoles")
    @ResponseBody
    public List<Role> getRoles(){
        return roleService.selectFindAll();
    }
    @RequestMapping("getTime")
    @ResponseBody
    public List<KeyAndValueVo> getTime(){
        return dictionaryService.getWorkTimes();
    }

    @RequestMapping("executionWorkTemplate")
    public String executionWorkTemplate(Integer id){
        return "work-template/executionWorkTemplate";
    }

    @RequestMapping("uploadNewExecutable")
    @ResponseBody
    public String uploadNewExecutable(@RequestBody List<WorkTemplateExecutable> workTemplateExecutables){
        try {
            workTemplateExecutableService.insertList(workTemplateExecutables);
            return "0";
        }catch (Exception e){
            e.printStackTrace();
            return "1";
        }

    }
}
